def playAudio(): time.sleep(1) CHUNK = 1024 # 从目录中读取语音
wf = wave.open("output_w.wav", 'rb')
data = wf.readframes(CHUNK) # 创建播放器
p = pyaudio.PyAudio() # 获得语音文件的各个参数
FORMAT = p.get_format_from_width(wf.getsampwidth()) CHANNELS = wf.getnchannels() RATE = wf.getframerate() logger.info('\nFORMAT: {} \nCHANNELS: {} \nRATE: {}'.format(FORMAT, CHANNELS, RATE)) logger.info("* playing") # 打开音频流, output=True表示音频输出
stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, frames_per_buffer=CHUNK,
logger.info("* playing1") while len(data) > 0: stream.write(data) data = wf.readframes(CHUNK) # p.terminate() logger.info("* done playing")
评论